home *** CD-ROM | disk | FTP | other *** search
- /*
- * Sample program of ways for computing with numbers larger than normal
- * written in Turbo C. This program computes powers of '2'. 'N' is the
- * variable holds the power to which the number is being raised (i.e. 2^N)
- */
- #include <time.h>
- #include <dos.h>
-
- typedef int number[10000];
-
- number arr;
- int x, i, place, N, tens, ones;
- char bell;
- struct time start, end;
-
-
- void Roundoff()
- {
- for (i = 0; i <= place - 1; i++)
- {
- while (((arr[i] / tens) > 0))
- {
- arr[i + ones] = arr[i + ones] + ((arr[i] / tens) % 10);
- ones = ones + 1;
- tens = tens * 10;
- }
- arr[i] = arr[i] % 10;
- ones = 1;
- tens = 10;
- }
- }
-
- void Multiply()
- {
- for (i = 0; i <= place; i++)
- arr[i] = arr[i] * 2;
- /* Change the '2' in previous line to compute the power of a different number.
- */
- }
-
- void Setplace()
- {
- if ((arr[place - 1] != 0) && (place < 10000)) place = place + (x * arr[place - 1] / 10);
- }
-
-
- main(argc,argv)
- int argc;
- char *argv[];
- { if (argc > 1)
- N = atoi(argv[1]);
- else
- { printf("\n");
- printf("Program to Calculate 2^N for up to 10000 digits in final answer\n");
- printf("\n");
- printf("Please enter the power of 2 to calculate\n");
- printf("N = ? ");
- scanf("%d",&N); }
- for (i = 0; i <= 10000; i++) arr[i] = 0;
- place = 4;
- tens = 10;
- ones = 1;
- bell = '\007';
- printf("%c\n",bell);
- gettime(&start);
- arr[0] = 1;
- for (x = 1; x <= N; x++)
- {
- Multiply();
- Roundoff();
- Setplace();
- }
- gettime(&end);
- printf("Start time was %02d:%02d:%02d:%02d\n", start.ti_hour,
- start.ti_min, start.ti_sec, start.ti_hund);
- printf("Finish time was %02d:%02d:%02d:%02d\n", end.ti_hour,
- end.ti_min, end.ti_sec, end.ti_hund);
- printf("2^%d = \n",N);
- while (arr[place] == 0) place = place - 1;
- for (i = 0; i <= place; i++)
- {
- printf("%d",arr[place - i]);
- if (((place - i) % 3 == 0) && (i < place)) printf(",");
- if ((place - i) % 54 == 0) printf("\n");
- }
- printf("\n");
- printf("%d digits%c\n",place + 1,bell);
- }
-